//-----------------------------------------------------------------------------
// Copyright (c) 2011 David Waby
// Initially created on 01/11/2011
//-----------------------------------------------------------------------------

#include "Global.h"
#include "Parser.h"
#include "Map.h"
#include "Tile.h"
#include "TileAttributePortal.h"
#include "MapStore.h"
#include <string>
#include <fstream>
#include "ItemStore.h"

//-----------------------------------------------------------------------------
Parser::Parser()
{
} 

//-----------------------------------------------------------------------------
Parser::~Parser()
{
} 

//-----------------------------------------------------------------------------
bool Parser::ParseMapStore(MapStore * o_mapStore)
{
	std::ifstream ifs("./Data/levels.json");    
	std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());

	Json::Value root;
	Json::Reader reader;
	bool loadOkay = reader.parse(str, root);
	if (!loadOkay)
	{
		return false;
	}

	o_mapStore->Deserialise(&root);

	return true;
}

//-----------------------------------------------------------------------------	
bool Parser::ParseItemStore(ItemStore * o_itemStore)
{
	std::ifstream ifs("./Data/items.json");    
	std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());

	Json::Value root;
	Json::Reader reader;
	bool loadOkay = reader.parse(str, root);
	if (!loadOkay)
	{
		return false;
	}

	o_itemStore->Deserialise(&root);

	return true;
}

//-----------------------------------------------------------------------------	
bool Parser::ParseAndCreateMap(Map ** o_map, std::string const * i_name)
{
	(*o_map) = new Map();

	std::ifstream ifs(i_name->c_str());    
	std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
	
	Json::Value root;
	Json::Reader reader;
	bool loadOkay = reader.parse(str, root);
	if (!loadOkay)
	{
		return false;
	}

	(*o_map)->Deserialise(&root);

	return true;
}